package Ptolemaiosz;

import java.util.ArrayList;

public class Physics {

	private static float gravity = -70.0f;
	private Level level;
	private long lastTime;
	private long currentTime;

    public Physics()
        {
            SkeletonLogger.logCall();
	    SkeletonLogger.logReturn();
        }               

    public void update(int time)
        {
            //Levelpartok lekerese
            ArrayList<LevelPart> levelParts = level.getLevelParts();
            
            //Minden levelpartra
            for(int i = 0; i < levelParts.size();i++)
                {
                    //Lekerjuk az adott levelparton levo fizikai entitasokat
                    ArrayList<PhysicalEntity> physicalEntities = level.getPhysicalEntities(levelParts.get(i));
                    
                    //Minden megkapott fizikia entitasra
                    for(int j = 0; j < physicalEntities.size();j++)
                        {
                            //Ertesitjuk az adott fizikai entitast az ido mulasarol
                            physicalEntities.get(j).update(time);
                        }
                }
		
            //vegul utkozest keresunk a frissitett poziciokkal
           collisionDetection();
	}

    public void collisionDetection()
        {
            
            //Levelpartok lekerese
            ArrayList<LevelPart> levelParts = level.getLevelParts();
            
            //Minden levelpartra
            for(int i = 0; i < levelParts.size();i++)
                {
                    //Lekerjuk az adott levelparton levo fizikai entitasokat
                    ArrayList<PhysicalEntity> physicalEntities = level.getPhysicalEntities(levelParts.get(i));
                    
                    //Minden levelpartra paronkent utkozest keresunk
                    for(int k = 0; k < physicalEntities.size(); k++)
                        {
                            for(int l = 0; l < physicalEntities.size(); l++)
                        	{
                                    if(k != l) //Ha nem onmagaval akarnank utkoztetni
                                        {
                        		    //Lekerjuk a ket entitas tulajdonsagait
                                            PhysicalEntity p1 = physicalEntities.get(k);
                                            PhysicalEntity p2 = physicalEntities.get(l);
                        			
                                                Vector pos1 = p1.getPosition();
                                                Vector pos2 = p2.getPosition();
                                               
                                                CustomPolygon poly1 = p1.getPolygon();
                                                CustomPolygon poly2 = p2.getPolygon();
                                            
                                                
                                            //System.err.println(p1 + " es " + p2 + " utkoztetese");
                                            
                                            //utkozest vizsgalunk
                                            boolean collision = CustomPolygon.isCollided(pos1, poly1, pos2, poly2);
                                            
                                            if(collision == true)
                                                {                                                   
                                                    
                                                    //ha utkozes tortent, akkor meghivjuk az utkozteto fuggvenyeket
                                                    p1.collision(p2);
                                                    //Nem kell majd kesobb meghivja a masikra az egyiket, ha ugy jonnek az indeyek. Rajzold le, ha bnem hiszed!
                                                    //p2.collision(p1);
                                                }                               
                                        }
                                }
                        }
                }
	}

	/**
	 * 
	 * @param level
	 */
	public void setLevel(Level level) {
		SkeletonLogger.logCall();
		
		SkeletonLogger.logReturn();
		
		this.level = level;
	}

	public void timer() {
		SkeletonLogger.logCall();
		
			
		SkeletonLogger.logReturn();
	}
        
        public static float getGravity()
        {
            SkeletonLogger.logCall();
            SkeletonLogger.logReturn();
            return gravity;
        }

}